package com.hntx.joymusic.util;

import android.app.Activity;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.hntx.joymusic.R;

/**
 * Created by guo on 2018/1/2.
 */

public class DialogUtils {

    static PopupWindow popWnd = null;

    public static void popupWindowShow(Activity context, int rid) {
        try {
            if (popWnd == null)
                popWnd = new PopupWindow(context);
            if (popWnd.isShowing()) return;
            final View contentView = LayoutInflater.from(context).inflate(R.layout.popuplayout, null);
            popWnd.setContentView(contentView);
            popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (!context.isFinishing() || !context.isDestroyed())
                    contentView.post(() -> popWnd.showAtLocation(contentView, Gravity.CENTER, 0, 0));
            }
//        popWnd.showAtLocation(contentView, Gravity.CENTER, 0, 0);
            ImageView imageView = contentView.findViewById(R.id.con_img);
            imageView.setImageResource(rid);
            contentView.postDelayed(() -> popWnd.dismiss(), 3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
